﻿using DeviceMonitor.Dao;
using DeviceMonitor.Model;
using MPMS.Srv.WebService;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace DeviceMonitor.Utils
{
    public class AlarmUtils
    {
        public static void DoUploadAlarm()
        {
            Boolean done = false;
            while (!done)
            {
                try
                {
                    //MessageBox.Show("进入查询");
                    //查询时间
                    //string endTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //String lastQueryTime = (String)SqlMapDao.instance().QueryForObject("selectAlarmLastQueryTime", null);

                    JObject paramJSON = new JObject();
                    //MessageBox.Show(lastQueryTime);
                    //string url = "http://" + App.GetConfig().DBServiceIp + ":" + App.GetConfig().DBServicePort + "/queryAlarms";
                    string url = "http://" + App.GetConfig().DBServiceIp + ":" + App.GetConfig().DBServicePort + "/alerm/queryAlermList";

                    //paramJSON.Add("queryStartDate", lastQueryTime);
                    //paramJSON.Add("queryEndDate", endTime);
                    //paramJSON.Add("queryStartDate", "2022-11-19 20:00:19");
                    //paramJSON.Add("queryEndDate", "2022-11-19 20:05:19");
                    //url += "?queryStartDate=" + lastQueryTime;
                    //url += "&queryEndDate=" + endTime;
                    String response = HttpPostHelper.doHttpPost(url, paramJSON.ToString());
                    JObject respJson = JObject.Parse(response);

                    JObject alarmJsonObj = JObject.Parse(response);
                    JObject json = (JObject)respJson.GetValue("data");
                    JArray detial = (JArray)json.GetValue("dataList"); ;

                    string lastQueryTime = (string)json.GetValue("lastQueryTime");
                    string endTime = (string)json.GetValue("currentQueryTime");


                    JArray alarms=new JArray();
                    if (alarmJsonObj["code"].ToString().Equals("200"))
                    {
                        alarms = detial;
                        //MessageBox.Show("查询成功");
                    }
                    else
                    {
                        return;
                    }
                    int num=0;
                    if (alarms == null || !alarms.HasValues)
                    {
                        return;
                    }
                    foreach (JObject alarm in alarms)
                    {
                        string alarmTime = (string)alarm["createTime"];
                        //string alarmWord = (string)alarm["alarmword"];
                        string alarmName = (string)alarm["alermName"];
                        string newalarm = (string)alarm["alerm"];
                        string id=(string)alarm["id"];
                        string position = "";
                        if (alarm["deviceId"].ToString().Equals("39"))
                        {
                            position = "31";
                        }
                        else
                        {
                            position = (int.Parse(alarm["deviceId"].ToString())-4).ToString();
                        }
                        string carNumber = (string)alarm["carNumber"];

                        //if ("00000000".Equals(alarmWord)) continue;
                        //for(int i=0; i < alarmWord.Length; i++)
                        //{
                            //string word = alarmWord.Substring(i, 1);
                            //if (!"1".Equals(word)) continue;

                            try
                            {
                            if (carNumber!=null&& !carNumber.Equals("") )
                            {
                                //Hashtable paramTable = new Hashtable();
                                //JObject paramJson = new JObject();
                                //paramJson.Add("carNumber", carNumber);
                                //paramJson.Add("alarmTime", alarmTime);
                                //paramTable.Add("carNumber", carNumber);
                                //paramTable.Add("alarmTime", alarmTime);
                                //int count = (int)SqlMapDaoNew.instance().QueryForObject("selectBillCount", paramTable);
                                //if (count > 0)
                                //{
                                String uuid  = getUUID();
                                AlermModel alermModel = new AlermModel();
                                JObject alermJson = new JObject();

                                alermJson.Add("id", uuid);
                                alermJson.Add("moduleId", id);
                                alermJson.Add("groupCode", "0");
                                alermJson.Add("code", "0");
                                alermJson.Add("deviceId", position);
                                alermJson.Add("alerm", newalarm);
                                alermJson.Add("alermName", alarmName);
                                alermJson.Add("createTime", alarmTime);
                                alermJson.Add("billNo","");
                                //alermModel.id = uuid;
                                //alermModel.moduleId = id;
                                //alermModel.groupCode = "0";
                                //alermModel.code = "0";
                                //alermModel.deviceId = position;
                                //alermModel.alerm = newalarm;
                                //alermModel.alermName = alarmName;
                                //alermModel.billNo = "";
                                DockingInfo dockingInfo = new DockingInfo();
                                string resp = dockingInfo.AlarmInfo(
                                      uuid,
                                      id,
                                      "0",
                                      "东营齐润化工有限公司",
                                      carNumber,
                                      alarmTime,
                                      position,
                                      newalarm
                                      );
                                if (resp.Equals("true"))
                                {
                                    alermJson.Add("code", "1");
                                    //MessageBox.Show("上传成功");
                                    if (alarms.Count() > 0)
                                    {
                                        JObject uploadTime = new JObject();
                                        uploadTime.Add("currentQueryTime", endTime);
                                        uploadTime.Add("type", "alerm");
                                        string timeUrl = "http://" + App.GetConfig().DBServiceIp + ":" + App.GetConfig().DBServicePort + "/alerm/updateLastQueryTime";
                                        String uploadResponse = HttpPostHelper.doHttpPost(timeUrl, uploadTime.ToString());

                                    }
                                }
                                string alermUrl = "http://" + App.GetConfig().DBServiceIp + ":" + App.GetConfig().DBServicePort + "/alerm/insertAlerm";
                                String billResponse = HttpPostHelper.doHttpPost(alermUrl, alermJson.ToString());
/*    切换上传顺序                            DockingInfo dockingInfo = new DockingInfo();
                                string resp = dockingInfo.AlarmInfo(
                                      uuid,
                                      id,
                                      "0",
                                      "东营齐润化工有限公司",
                                      carNumber,
                                      alarmTime,
                                      position,
                                      newalarm
                                      );
                                if (resp.Equals("true"))
                                {
                                    //MessageBox.Show("上传成功");
                                    if (alarms.Count() > 0)
                                    {
                                        JObject uploadTime = new JObject();
                                        uploadTime.Add("currentQueryTime", endTime);
                                        uploadTime.Add("type", "alerm");
                                        string timeUrl = "http://" + App.GetConfig().DBServiceIp + ":" + App.GetConfig().DBServicePort + "/alerm/updateLastQueryTime";
                                        String uploadResponse = HttpPostHelper.doHttpPost(timeUrl, uploadTime.ToString());

                                    }
                                }*/
                                }
                            //}
                            
                               
                           
                        }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.ToString());
                            done = true;
                        }
                        //}

                    }
                    
                    
            
                }
                catch (Exception ex)
                {
                    LogObject.WriteLog(ex.ToString(), "Message");
                    done = true;
                }
                done = true;
                //Thread.Sleep(5 * 1000 * 60);
            }
        }
        public static String getUUID()
        {
            return System.Guid.NewGuid().ToString().Replace("-", "");
        }
    }
}
